package org.openapitools.controller;

import org.openapitools.common.Result;
import org.openapitools.model.Dog;
import org.openapitools.service.DogService;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 自定义示例管理
 * @author lemon
 */
@RequestMapping("/test/dog")
@RestController
public class DogControllers {

    private final DogService dogService;

    public DogControllers(DogService dogService) {
        this.dogService = dogService;
    }

    /**
     * 添加示例
     * @param dog 示例信息
     * @return 添加的示例
     */
    @PostMapping
    public Result<Dog> addPet(@RequestBody(required = false) Dog dog) {
        Dog addedDog = dogService.addPet(dog);
        return Result.success(addedDog);
    }

    /**
     * 根据ID获取示例
     * @param id 示例ID
     * @return 示例列表
     */
    @GetMapping("/getById")
    public Result<List<Dog>> getPet(@RequestParam String id) {
        List<Dog> dogs = dogService.getPet(id);
        return Result.success(dogs);
    }

    /**
     * 根据name获取示例
     * @param name 示例名称
     * @return 示例列表
     */
    @GetMapping("/getByName")
    public Result<List<Dog>> getPetByName(@RequestParam String name) {
        List<Dog> dogs = dogService.getPetByName(name);
        return Result.success(dogs);
    }

    /**
     * 获取所有示例
     * @return 所有示例列表
     */
    @GetMapping
    public Result<List<Dog>> getPets() {
        List<Dog> dogs = dogService.getPets();
        return Result.success(dogs);
    }

    /**
     * 更新示例信息
     * @param dog 示例信息
     * @return 更新后的示例
     */
    @PutMapping
    public Result<Dog> updatePet(@RequestBody(required = false) Dog dog) {
        Dog updatedDog = dogService.updatePet(dog);
        return Result.success(updatedDog);
    }

    /**
     * 删除示例
     * @param petId 示例ID
     * @return 删除结果
     */
    @DeleteMapping("/{petId}")
    public Result<String> deletePet(@PathVariable String petId) {
        boolean deleted = dogService.deletePet(petId);
        if (deleted) {
            return Result.success("示例删除成功");
        } else {
            return Result.error("示例删除失败");
        }
    }
}
